package com.yocard.yxyd.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.widget.TextView;

import com.yocard.yxyd.R;
import com.yocard.yxyd.http.model.response.RechargesBean;
import com.yocard.yxyd.utils.BigDecimalUtil;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created on 2019/4/9.
 * 充值额度适配器
 */
public class TopupAadapter extends RecyclerView.Adapter<TopupAadapter.ViewHolder> {

    int clickPos;
    private List<RechargesBean> rechargesBeanList;
    private ValueCallback<String> callback;

    public TopupAadapter(List<RechargesBean> rechargesBeanList) {

        this.rechargesBeanList = rechargesBeanList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_top_up, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        if (i == clickPos) {
            viewHolder.itemView.setSelected(true);
        } else {
            viewHolder.itemView.setSelected(false);
        }

        RechargesBean itemBean = rechargesBeanList.get(i);
        viewHolder.tvCoin.setText(BigDecimalUtil.getFormatCoin(itemBean.getCoin()));
        String amount = BigDecimalUtil.divide(new BigDecimal(itemBean.getAmount())
                , new BigDecimal("100")).toString();
        viewHolder.tvAmount.setText("¥ " + amount);
        viewHolder.itemView.setTag(i);
        viewHolder.itemView.setOnClickListener(clickListener);
    }

    @Override
    public int getItemCount() {
        return rechargesBeanList == null ? 0 : rechargesBeanList.size();
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            clickPos = (int) v.getTag();
            notifyDataSetChanged();

            if(callback != null){
                callback.onReceiveValue(getSelectedAmount());
            }
        }
    };

    public void setOnItemclick(ValueCallback<String> callback){

        this.callback = callback;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_coin)
        TextView tvCoin;
        @BindView(R.id.tv_amount)
        TextView tvAmount;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public String getSelectedAmount(){
        String amount = "";
        if(rechargesBeanList.size() >0){
            amount = rechargesBeanList.get(clickPos).getAmount();
            amount = BigDecimalUtil.divide(new BigDecimal(amount)
                    , new BigDecimal("100")).toString();
        }
        return amount;
    }

    public String getSelectId(){
        return rechargesBeanList.get(clickPos).getId();
    }
}
